/**
 *  Copyright (C) 2008-2013  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *          http://www.gnu.org/licenses/lgpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.telosys.persistence.database;

import java.sql.Connection;
import java.util.Properties;

import org.telosys.persistence.sql.SqlTool;


/**
 * Database configuration loaded from the configuration file
 * 
 * @author Laurent GUERIN
 *  
 */
public class DatabaseConfig
{
	private final static int    DEFAULT_ISOLATION_LEVEL = Connection.TRANSACTION_READ_COMMITTED ;
	
	private final static String DEFAULT_SQL_FOLDERS [] = {"sql"} ;
	
    private int        _id                 = 0;

    private String     _sName              = null ;

    private String     _sDriver            = null ;

    private String     _sUrlJdbc           = null ;

    private String     _sIsolationLevel    = null ; // 0 (TRANSACTION_NONE), 1, 2, 4, 8
    private int        _isolationLevel     = DEFAULT_ISOLATION_LEVEL ;

    private int        _iPoolSize          = 0 ;

//    private String     _sDAORegistryClass  = null ; // v 1.0.0

//    private String     _sDAOClassPattern    = null ; // v 1.0.0
    
    private String     _sDataSource          = null ; // v 2
    private String     _sCustomClass         = null ; // v 2
    private Properties _properties           = null ; // v 2
    private String     _sDbFeaturesClassName = null ; // v 2

    private String     _sRecordClassPattern = null ; // v 1.0.0
    
    private String     _sMappingClassPattern = null ; // v 2.0.0

    private String[]   _sqlFolders = DEFAULT_SQL_FOLDERS ; // v 2.0.0

    //-----------------------------------------------------------------------------
    // Constructor(s)
    //-----------------------------------------------------------------------------
    //public Database(int id, String sName, String sDriver, String sUrl, String sIsolationLevel, String sPoolSize )
    /**
     * Constructor 
     */
    protected DatabaseConfig(int id, String sName, String sDriver, String sUrl, String sIsolationLevel, 
    		int poolSize, 
            String datasource, 
            String customClass, 
            Properties properties,
            String dbFeaturesClassName, 
            
            String sRecordClassPattern, 
            String sMappingClassPattern,
            String[] sqlFolders )
    {
        //trace("Constructor ...");
        _id = id;
        _sName = sName;
        
        _sDriver = sDriver;
        _sUrlJdbc = sUrl;
        _sIsolationLevel = sIsolationLevel;
        _isolationLevel  = SqlTool.getIsolationLevel(sIsolationLevel, DEFAULT_ISOLATION_LEVEL );        
        
        _iPoolSize    = poolSize ;
        _sDataSource  = datasource ;
        _sCustomClass = customClass ;
        _properties   = properties ;
        _sDbFeaturesClassName = dbFeaturesClassName ;
        _sRecordClassPattern = sRecordClassPattern ;

        _sMappingClassPattern = sMappingClassPattern ; // v 2.0
        
        if ( sqlFolders != null ) {
            _sqlFolders = sqlFolders ; // v 2.0
        }
        else {
        	_sqlFolders = DEFAULT_SQL_FOLDERS ;
        }
    }

    //-----------------------------------------------------------------------------
    // Getters
    //-----------------------------------------------------------------------------
    /**
     * Returns the database ID ( from 0 to max id )
     * @return
     */
    public int getId()
    {
        return _id;
    }
    
    /**
     * Returns the database name
     * @return
     */
    public String getName()
    {
        return _sName;
    }

    /**
     * Returns the JDBC driver used for this database
     * @return
     */
    public String getDriver()
    {
        return _sDriver;
    }
    
    /**
     * Returns the JDBC URL used for this database
     * @return
     */
    public String getJdbcURL()
    {
        return _sUrlJdbc;
    }

    /**
     * Returns the original 'isolation level' parameter 
     * @return
     */
    public String getIsolationLevelString()
    {
        return _sIsolationLevel;
    }
    
    /**
     * Returns the 'isolation level' to be used
     * @return
     */
    public int getIsolationLevel()
    {
        return _isolationLevel;
    }
    
    
    public Properties getProperties()
    {
        return _properties ;
    }
    
//    /**
//     * Returns the original 'pool size' parameter 
//     * @return
//     */
//    public String getPoolSize()
//    {
//        return _sPoolSize;
//    }

//    /**
//     * Returns the 'pool size' parameter value <br>
//     * ( returns 0 if the original parameter string cannot be converted to int ) 
//     * @return
//     */
//    public int getPoolSizeValue()
//    {
//        return StrUtil.getInt(_sPoolSize, 0);
//    }
    public int getPoolSize()
    {
        return _iPoolSize ;
    }
    
//    /**
//     * Returns the DAO registry class name or null if none 
//     * @return
//     * @since v 1.0.0
//     */
//    public String getDAORegistryClass() 
//    {
//        return _sDAORegistryClass ;
//    }

//    /**
//     * Returns the DAO class name pattern or null if none 
//     * @return
//     * @since v 1.0.0
//     */
//    public String getDAOClassPattern()
//    {
//        return _sDAOClassPattern ;
//    }
    
    /**
     * Returns the DataSource name or null if none 
     * @return
     * @since v 2.0.0
     */
    public String getDataSourceName()
    {
        return _sDataSource ;
    }
    
    /**
     * Returns the Connection Provider custom class name or null if none 
     * @return
     * @since v 2.0.0
     */
    public String getConnectionProviderCustomClass()
    {
        return _sCustomClass ;
    }
    
    /**
     * Returns the DbFeatures class name (or null if none) 
     * @return
     * @since v 2.0.0
     */
    public String getDbFeaturesClassName()
    {
        return _sDbFeaturesClassName ;
    }
    
    
    /**
     * Returns the record class name pattern or null if none 
     * @return
     * @since v 1.0.0
     */
    public String getRecordClassPattern()
    {
        return _sRecordClassPattern ;
    }
    
    /**
     * Returns the mapping class pattern or null if none<br>
     * i.e. : "org.demo.vo.daomapping.${BEANNAME}Mapping"
     * @return the pattern
     * @since v 2.0.0
     */
    public String getMappingClassPattern()
    {
        return _sMappingClassPattern ;
    }
    
    /**
     * Returns the SQL folders ( folders where the SQL requests are located )
     * @return 
     * @since v 2.0.0
     */
    public String[] getSqlFolders()
    {
        return this._sqlFolders ;
    }
    //-----------------------------------------------------------------------------
    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    public String toString()
    {
        return _id + " : '" + _sName + "' (" + _sDriver + ", '" + _sUrlJdbc + "', " + _sIsolationLevel + ", " + _iPoolSize + ")";
    }
}